package com.mzw.project.bizmq;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzw.project.common.ErrorCode;
import com.mzw.project.exception.BusinessException;
import com.mzw.project.mapper.VoucherOrderMapper;
import com.mzw.project.model.entity.VoucherOrder;
import com.mzw.project.service.VoucherOrderService;
import com.mzw.project.service.VoucherService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * @author L1nAn
 */
@Component
@Slf4j
public class BiMessageDelayedConsumer {

    @Resource
    VoucherOrderMapper voucherOrderMapper;
    @SneakyThrows
    @RabbitListener(queues = {BiMqConstant.DELAYED_QUEUE_NAME}, ackMode = "MANUAL")
    public void receiveMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        String[] splits = message.split(",");
        long orderId = Long.parseLong(splits[1]);
        boolean b = voucherOrderMapper.updateStatus(orderId);
        if (!b) {
            channel.basicNack(deliveryTag, false, false);
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR);
        }
        channel.basicAck(deliveryTag, false);
    }
}